﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Text.RegularExpressions;
using FinPlusInterfaces;
using FinPlusAssembler;

namespace FinPlusCompView
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValueShiftTextBox : FinPlusControl
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Test = true)]
        public override string TestString { get { return string.Format("{0}|{1}", IsEnabled, Text); } }
        [State(Test = true)]
        public string ViewString { get { return string.Format("{0}|{1}", textBox.IsEnabled, textBox.Text); } }

        public override string Id { get { return GetValue(IdProperty).ToString(); } set { } }
        public string Value { get { return GetValue(ValueProperty).ToString(); } set { } }
        public string Text { get { return GetValue(TextProperty).ToString(); } set { SetValue(TextProperty, value); } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }
        public object Step { get { return GetValue(StepProperty); } }
        public string StringFormat { get { return GetValue(StringFormatProperty).ToString(); } }
        public Regex InputMask { get { return (Regex)GetValue(InputMaskProperty); } }

        public static readonly DependencyProperty IdProperty = DependencyProperty.Register(PodField.Id.ToString(), typeof(string), typeof(FinPlusValueShiftTextBox));
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(BeanField.Value.ToString(), typeof(string), typeof(FinPlusValueShiftTextBox));
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(Propertys.Text.ToString(), typeof(string), typeof(FinPlusValueShiftTextBox), new UIPropertyMetadata(Callback));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValueShiftTextBox));
        public static readonly DependencyProperty StepProperty = DependencyProperty.Register(Propertys.Step.ToString(), typeof(object), typeof(FinPlusValueShiftTextBox));
        public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register(Propertys.StringFormat.ToString(), typeof(string), typeof(FinPlusValueShiftTextBox));
        public static readonly DependencyProperty InputMaskProperty = DependencyProperty.Register(Propertys.InputMask.ToString(), typeof(Regex), typeof(FinPlusValueShiftTextBox));

        private enum Propertys {Text, IsEnabled, Step, StringFormat, InputMask }
        private bool _hasSetEnable;

        //construct
        public FinPlusValueShiftTextBox()
        {
            InitializeComponent();
            IsValid = true;

            textBox.Cursor = ((TextBlock)this.Resources["CHit"]).Cursor;
        }

        //common control interface
        public override void CmdExecute(IPod command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.ValueShiftUp: ValueShift(true); break;
                    case Cmds.ValueShiftDown: ValueShift(false); break;
                    default: throw new Exception(string.Format("cmd not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = Level.Error.Log(Id, Config, e.Message, e);
            }
        }

        //private
        private bool ValueShift(bool isUp)
        {
            double v;
            var s = (double)GetValue(StepProperty);
            if( double.TryParse(Text, out v))
                Text = (v + (isUp ? s : -s)).ToString(StringFormat);
            return true;
        }

        private void SetControl(DependencyPropertyChangedEventArgs a)
        {
            //permission
            if (!_hasSetEnable)
            {
                if (!IsEnabled || !Permissions.Get(PodField.Id.ToString(), Id).CanWrite)
                {
                    editText.IsReadOnly = true;
                    bUp.IsEnabled = bDown.IsEnabled = false;
                }
                else
                {
                    editText.IsReadOnly = false;
                    bUp.IsEnabled = bDown.IsEnabled = true;
                }
                _hasSetEnable = true;
            }

            //format
            double value;
            if (double.TryParse(a.NewValue.ToString(), out value))
                editText.Foreground = textBox.Foreground = value < 0 ? Brushes.Red : Brushes.Black;
        }

        //cmds
        private enum Cmds { ValueShiftUp, ValueShiftDown, ValuShiftLevel }

        //events
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueShiftTextBox)d).SetControl(a); }
        private void Control_LostFocus(object s, RoutedEventArgs a)
        {
            editBox.Visibility = Visibility.Collapsed;
            textBox.Visibility = Visibility.Visible;
        }
        private void Text_MouseDown(object s, MouseButtonEventArgs a)
        {
            if (a.ClickCount != 2) return;

            textBox.Visibility = Visibility.Collapsed;
            editBox.Visibility = Visibility.Visible;
        }
        private void Text_PreviewKeyDown(object s, KeyEventArgs a) { }//TODO
        private void Text_PreviewTextInput(object s, TextCompositionEventArgs a) { if (InputMask != null && !InputMask.IsMatch(a.Text)) a.Handled = true; }
    }
}
